package com.auxteam.elope;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.MenuItem;
import com.auxteam.elope.activities.SampleListFragment;
import com.auxteam.elope.activities.Util;
import com.auxteam.elope.menus.DynamicMenuList;
import com.auxteam.elope.menus.SampleAdapter;
import com.auxteam.elope.menus.SampleItem;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

public class CustomHomeScreen extends SherlockFragmentActivity implements OnItemClickListener{

	private SlidingMenu menu;
	DynamicMenuList menuList;
	Intent i;
	String username;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setTitle(R.string.app_name);

		// set the Above View
		setContentView(R.layout.content_frame);
		getSupportFragmentManager()
		.beginTransaction()
		.replace(R.id.content_frame, new SampleListFragment())
		.commit();
		i = this.getIntent();
		username = i.getStringExtra("username");
		Toast.makeText(getApplicationContext(), "Welcome " + username, Toast.LENGTH_SHORT).show();
		
		SampleAdapter adapter = new SampleAdapter(getApplicationContext());
		adapter.add(new SampleItem("Search", R.drawable.binoculars));
		adapter.add(new SampleItem(username, R.drawable.user));
		adapter.add(new SampleItem("My Stuff", R.drawable.lightbulb));
		adapter.add(new SampleItem("Shop", R.drawable.shopping_basket));
		adapter.add(new SampleItem("Player", R.drawable.play));
		adapter.add(new SampleItem("Settings", R.drawable.gear));
		adapter.add(new SampleItem("Exit", R.drawable.power));
		menuList = new DynamicMenuList();
		menuList.setListAdapter(adapter);
		
		// configure the SlidingMenu
		menu = new SlidingMenu(this);
		menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
		menu.setShadowWidthRes(R.dimen.shadow_width);
		menu.setShadowDrawable(R.drawable.shadow);
		menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
		menu.setFadeDegree(0.35f);
		menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
		menu.setMenu(R.layout.menu_frame);
		getSupportFragmentManager()
		.beginTransaction()
		.replace(R.id.menu_frame, menuList)
		.commit();
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
	}
	
	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		super.onPostCreate(savedInstanceState);
		menuList.getListView().setOnItemClickListener(this);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			menu.toggle();
			return true;
		case R.id.github:
			Util.goToGitHub(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	@Override
	public void onBackPressed() {
		if (menu.isMenuShowing()) {
			menu.showContent();
		} else {
			super.onBackPressed();
		}
	}
	
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
		if(position == 0){
			Toast.makeText(getApplication().getApplicationContext(), "Search Clicked", Toast.LENGTH_SHORT).show();
		}
		if(position == 1){
			//Toast.makeText(getActivity().getApplicationContext(), "Search Clicked", Toast.LENGTH_SHORT).show();
			Toast.makeText(getApplicationContext(), "Hi " + username, Toast.LENGTH_SHORT).show();
		}
		if(position == 2){
			Toast.makeText(getApplicationContext(), "My Stuff Clicked", Toast.LENGTH_SHORT).show();
		}
		if(position == 3){
			Toast.makeText(getApplicationContext(), "Shop Clicked", Toast.LENGTH_SHORT).show();
		}
		if(position == 4){
			Toast.makeText(getApplicationContext(), "Player Clicked", Toast.LENGTH_SHORT).show();
		}
		if(position == 5){
			//Toast.makeText(getActivity().getApplicationContext(), "Search Clicked", Toast.LENGTH_SHORT).show();
			Toast.makeText(getApplicationContext(), "Settings Clicked", Toast.LENGTH_SHORT).show();
		}
		if(position == 6){
			//Toast.makeText(getActivity().getApplicationContext(), "Search Clicked", Toast.LENGTH_SHORT).show();
			finish();
		}
		
	}

}